package com.egao.orderB.service.impl;

import com.egao.common.core.damon.results.PageResult2;
import com.egao.common.core.web.JsonResult;
import com.egao.common.system.entity.User;
import com.egao.orderB.biz.EventsBiz;
import com.egao.orderB.biz.EventsNumBiz;
import com.egao.orderB.entity.Events;
import com.egao.orderB.entity.EventsNum;
import com.egao.orderB.service.EventsNumService;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

//import com.egao.orderB.mapper.EventsNumMapper;
//import javax.transaction.Transactional;

/**
 * 事件记录数量服务实现类
 * @author : Doman
 * @date : 2021-05-25 15:42:33
 * @version ：1.0
 */
@Service
public class EventsNumServiceImpl /*extends ServiceImpl<EventsNumMapper, EventsNum> */implements EventsNumService {

	@Autowired
	private EventsNumBiz eventsNumBiz;
	@Autowired
	private EventsBiz eventsBiz;

    /**
    @Override
    public PageResult<EventsNum> listPage(PageParam<EventsNum> page) {
        List<EventsNum> records = baseMapper.listPage(page);
        return new PageResult<>(records, page.getTotal());
    }

    @Override
    public List<EventsNum> listAll(Map<String, Object> page) {
        return baseMapper.listAll(page);
    } */

    @Override
    public JsonResult list(PageResult2<T> page, EventsNum eventsNum, User loginUser)  throws Exception{
        return eventsNumBiz.list(page,eventsNum,loginUser);
    }

    @Override
    public JsonResult list(EventsNum eventsNum, User loginUser) {
        return JsonResult.ok().setData(eventsNumBiz.list(eventsNum,loginUser));
    }

    @Override
    public JsonResult getById(EventsNum eventsNum) {
        return JsonResult.ok().setData(eventsNumBiz.getById(eventsNum));
    }

    @Override
    public Integer getByUserId(EventsNum eventsNum) {
        return eventsNumBiz.getByUserId(eventsNum);
    }

    @Transactional
    @Override
    public JsonResult save(EventsNum eventsNum, User loginUser) {
        if (eventsNumBiz.save(eventsNum, loginUser)) {
            return JsonResult.ok("添加成功");
        }
        return JsonResult.error("添加失败");
    }

    @Transactional
    @Override
    public JsonResult update(EventsNum eventsNum, User loginUser) {
        Events events = new Events();
        events.setCustomerId(eventsNum.getModule());
        int count = eventsBiz.getListNum(events);
        eventsNum.setNums(count);
        int eventNum = eventsNumBiz.getByUserId(eventsNum);
        if (eventNum == -1) {
            eventsNumBiz.save(eventsNum, loginUser);
        } else {
            eventsNumBiz.updateNum(eventsNum, loginUser);
        }
        return JsonResult.ok("修改成功");
    }

    @Transactional
    @Override
    public JsonResult remove(EventsNum eventsNum, User loginUser) {
        if (eventsNumBiz.remove(eventsNum, loginUser)) {
            return JsonResult.ok("删除成功");
        }
        return JsonResult.error("删除失败");
    }

}
